<template>
  <div>
    <div class="search-content" style="padding-bottom: 17px;">
      <el-form :model="searchContent" ref="searchFrom" :inline="true">
        <el-form-item label="申请时间：" prop="addTime">
          <el-date-picker
            v-model="searchContent.addTime"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="评估时间：" prop="pgTime">
          <el-date-picker
            v-model="searchContent.pgTime"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <!--el-form-item label="车牌：" prop="store_nameHandWhite">
          <el-input
            class="search-content-input"
            v-model="searchContent.store_nameHandWhite"
            placeholder="请输入车牌">
          </el-input>
        </el-form-item-->
        <el-form-item label="手机号：" prop="search_mobile">
          <el-input
            class="search-content-input"
            v-model="searchContent.search_mobile"
            placeholder="请输入手机号">
          </el-input>
        </el-form-item>
        <el-form-item label="评估状态：" prop="store_id">
          <el-select v-model="searchContent.store_id" placeholder="精准评估状态">
            <el-option
              v-for="item in options"
              :key="item.store_id"
              :label="item.store_name"
              :value="item.store_id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="有效性：" prop="isValid">
          <el-select v-model="searchContent.isValid" placeholder="有效性">
            <el-option
              v-for="item in validList"
              :key="item.valid_id"
              :label="item.valid_name"
              :value="item.valid_id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属集团：" prop="first_depart_id">
            <el-select 
                v-model="searchContent.first_depart_id" 
                placeholder="请选择集团">
                <el-option
                    v-for="item in groupList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button type="primary" @click="resetSearchData">重置</el-button>
        </el-form-item>
			</el-form>
    </div>
    <div class="search-list">
      <el-table :data="dataList" border :height="oHeight" header-cell-class-name="hdCellStyle">
        <el-table-column
          align="center"
          prop="member_name"
          label="用户名"
          min-width="130"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="car_info"
          label="车辆名称"
          min-width="180"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="gmt_create"
          label="创建日期"
          min-width="110"
        >
          <template slot-scope="scope">
            <span>{{
              (scope.row.gmt_create * 1000) | dateFormatter("YYYY-MM-DD ")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="plate_number"
          label="车牌"
          min-width="110"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="phone"
          label="手机"
          min-width="110"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="mile"
          label="里程数"
          min-width="110"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="time"
          label="上牌时间"
          min-width="120"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="Estimate"
          label="在线评估价格/万元"
          min-width="120"
        >
        </el-table-column>
        <el-table-column
          align="center"
          label="精准评估状态"
          min-width="110"
        >
          <template slot-scope="scope">{{scope.row.is_process == 2 ? '未评估' : '已评估'}}</template>
        </el-table-column>
        <el-table-column
          align="center"
          label="有效性"
          min-width="110"
        >
          <template slot-scope="scope">{{scope.row.isValid == 1 ? '有效' : '无效'}}</template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="source"
          label="来源"
          min-width="120"
					v-if="!externalData"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="store_id"
          label="店铺"
					:formatter="handleFormatter"
          min-width="120"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="depart_name"
					:formatter="handleFormatter"
          label="所属集团"
          min-width="110">
				</el-table-column>
        <el-table-column
          fixed="right"
          align="center"
          label="操作"
          key="operate"
          width="120"
					v-if="!externalData"
        >
          <template slot-scope="scope">
            <!-- <el-button
              v-show="scope.row.status == 1"
              class="operateBtn"
              type="text"
              @click="deleter(scope.row.id,2)"
              >通过|</el-button
            > -->
            <el-button
              v-show="scope.row.permision == 1||scope.row.is_process == 2"
              class="operateBtn"
              type="text"
              @click="showcheckDialog(scope.row)"
              >评估</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        :before-close="handlecheckClose"
        title="二手车评估"
        :visible="checkdialog"
        width="60%">
        <el-form  :model="form"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm" 
        >
          <el-form-item label="车牌号码：" >
            <el-input v-model="form.plate_number" @keyup.native="inputChange('account')" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="车　　型:：" >
            <el-input v-model="form.car_info" @keyup.native="inputChange('account')" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="里程/公里：" >
            <el-input v-model="form.mile" @keyup.native="inputChange('account')" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="上牌时间：" >
            <el-input v-model="form.time" @keyup.native="inputChange('account')" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="姓　　名：" >
            <el-input v-model="form.phone" @keyup.native="inputChange('account')" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="城　　市：" >
            <el-input v-model="form.area_infos" @keyup.native="inputChange('account')" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="价值/万元：" >
            <el-input v-model="form.eval_price"  ></el-input>
          </el-form-item>
          <el-form-item label="评估时间：" >
            <el-input v-model="i" @keyup.native="inputChange('account')" :disabled="true"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary"  @click="submit">提交</el-button>
        </span>
      </el-dialog>
      <div class="bottom">
        <div class="btn-group">
          <el-button
            size="mini"
            type="primary"
            class="btn-bottom"
            @click="expertData"
            >
            导出
          </el-button>
          <el-button
          v-show="false"
            size="mini"
            type="primary"
            class="btn-bottom"
            @click="showFn"
            >
            新增
          </el-button>
        </div>
        <!--分页部分-->
        <div class="paginationOne">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="searchContent.currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="searchContent.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <default-opr ref="ceshi"></default-opr>
  </div>
</template>
<script>
import { resize } from "@/common/js/height.js";
import { dateFormatter } from "../../../../common/js/util";
import defaultOpr from "./default-opr";
import Qs from "qs";

console.log("defaultOpr", defaultOpr);
export default {
	props: {
    externalData: { // 注意此属性不要删除，当此组件被单独引入第三方组件时候的父组件传值属性
			type: Object,
			default: null
		} 
	},
  components: { defaultOpr },
  data() {
    return {
      form: {

      },
      size:"",
      checkdialog:false,
      options: [],
      validList: [],
      oHeight: 800,
      searchContent: {
        addTime: "",
        pgTime:"",
        amount: "",
        cjj_add_points: "",
        config_type: "",
        id: "",
        is_open: "",
        points_model: "",
        points_ratio: "",
        start_date: "",
        store_add_points: "",
        store_id: "",
        store_nameHandWhite: "",
        search_mobile:"",
        update_time: "",
				first_depart_id: '',
        currentPage: 1,
        originate: '',
				pageSize: +localStorage.getItem('pageSize') || 10,
        filter_not_yt: null,
        isValid:"",
      },
      dataList: [],
			groupList: [],
      i:0,
      total: 0,
    };
  },
  created() {
		if(this.externalData && this.externalData.filter_not_yt) {
			this.searchContent.originate = this.externalData.originate;
			this.searchContent.filter_not_yt = this.externalData.filter_not_yt;
		}
    this.getList();
    this.getStoreListFun();
    this.handleGroup();
  },

  methods: {
			handleFormatter(row, column, cellValue) {
				if (cellValue != 0) return cellValue
			},
    handleGroup() {
        this.$http.post('/crm/coupons/index.php?act=statistics&op=getCouponDepart')
        .then(res => {
            if (res.data) {
                this.groupList = [
                    { label: '全部', value: 0 },
                    ...res.data.map(item => ({ 
                        label: item.depart_name, 
                        value: parseInt(item.depart_id) 
                    }))
                ]
            }
        }).catch(error => {
            error && this.$message.error(`${error}`)
        })
    },
    submit() {
      this.$http.post('/admin/activity/index.php?act=car_eval&op=ping', {
        id : this.form.id,
        dosubmit :1,
        eval_price:this.form.eval_price

				}).then(res => {
					if (res.code == 2) {

						this.handlecheckClose();
						this.getList();

					} else {

						alert(res.msg)
					}
				})

			},
    showcheckDialog(groupItem) {

      this.i=this.i+1;
      // console.log(this.i);
      this.i=new Date(parseInt(groupItem.gmt_create) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
				// this.$router.push(`/home/operate-panel/car-group/group-detail?id=${groupItem.groupId}`)
			// this.size="";
				this.form=groupItem;
				this.checkdialog = true;
    },
    handlecheckClose() {
			this.checkdialog = false;
			this.needCloseGroup = null;
		},
    storeLevel(level){
      switch (level) {
        case '1':
            return "未审核";
            break;
        case '2':
            return "已通过";
            break;
            default:
            return "未通过";
      }
    },
    ///新增按钮
    showFn(data) {
      this.$refs.ceshi.show_dialog_fn(data);
    },

    //// 导出操作

    expertData() {
      const data = {};
      data.page = this.searchContent.currentPage;
      data.eachNum = this.searchContent.pageSize;
      data.search_status = this.searchContent.store_id;
      if(this.searchContent.isValid != ""){
        data.isValid = Number(this.searchContent.isValid)
      }
      data.plate_number	 = this.searchContent.store_nameHandWhite;
      data.search_mobile	 = this.searchContent.search_mobile;
      data.first_depart_id	 = this.searchContent.first_depart_id;
      if(this.searchContent.filter_not_yt) data.filter_not_yt = this.searchContent.filter_not_yt;
      if (this.searchContent.originate) {
        data.originate = this.searchContent.originate
      }
      if (this.searchContent.addTime) {
        data.gmt_create_start = dateFormatter(
          //"YYYY-MM-DD hh:mm:ss",
          "YYYY-MM-DD 00:00:00",
          this.searchContent.addTime[0]
        );
        data.gmt_create_end = dateFormatter(
          "YYYY-MM-DD 23:59:59",
          this.searchContent.addTime[1]
        );
      }
      if (this.searchContent.pgTime) {
        data.eval_time_start = dateFormatter(
          "YYYY-MM-DD 00:00:00",
          this.searchContent.pgTime[0]
        );
        data.eval_time_end = dateFormatter(
          "YYYY-MM-DD 23:59:59",
          this.searchContent.pgTime[1]
        );
      }
      data.export	 = "yes";
      if (this.$store.state.isServer) {
        var host = location.host.replace("admin", "api");
      } else {
        var host = location.host + "/api";
      }

      var url =
        location.protocol +
        "//" +
        host +
        "/admin/activity/index.php?act=car_eval&op=list";
      var searchData = data;


      for (var key in searchData) {
        if (
          !(
            searchData[key] === null ||
            searchData[key] === "" ||
            searchData[key] === undefined ||
            key === "page" ||
            key === "pageSize" ||
            key === "currentPage"
          )
        ) {
          url += "&" + key + "=" + searchData[key];
        }
      }
      console.log(url);
      window.location.href = url;
    },

    // 选店铺id的列表
    getStoreListFun() {
        this.options =[{"store_id":"2","store_name":"未评估"},{"store_id":"0,1","store_name":"已评估"}];
        this.validList = [{"valid_id":"","valid_name":"全部"},{"valid_id":"1","valid_name":"有效"},{"valid_id":"2","valid_name":"无效"}]
    },
    handleSizeChange(pageSize) {
      this.searchContent.pageSize = pageSize;
      localStorage.setItem('pageSize', pageSize);
      this.searchContent.currentPage = 1;
      this.getList();
    },
    handleCurrentChange(page) {
      this.searchContent.currentPage = page;
      this.getList();
    },
    resetSearchData() {
      this.searchContent.export = "";
      this.searchContent.state = "";
      this.searchContent.store_nameHandWhite = "";
      this.searchContent.search_mobile = "";
      this.searchContent.isValid = "";
      this.searchContent.store_id = "";
      this.searchContent.addTime = "";
      this.searchContent.pgTime="";
			this.searchContent.first_depart_id = '';
      this.getList();
    },

    ////// 店铺详情总列表
    getList() {
      const data = {};
      data.currentPage = this.searchContent.currentPage;
      data.eachNum = this.searchContent.pageSize;
      data.search_status = this.searchContent.store_id;
      data.plate_number	 = this.searchContent.store_nameHandWhite;
      data.search_mobile	 = this.searchContent.search_mobile;
      data.first_depart_id	 = this.searchContent.first_depart_id;
      if(this.searchContent.filter_not_yt) data.filter_not_yt = this.searchContent.filter_not_yt;

      if (this.searchContent.originate) {
        data.originate = this.searchContent.originate
      }
      if (this.searchContent.addTime) {
        data.gmt_create_start = dateFormatter(
          "YYYY-MM-DD 00:00:00",
          this.searchContent.addTime[0]
        );
        data.gmt_create_end = dateFormatter(
          "YYYY-MM-DD 23:59:59",
          this.searchContent.addTime[1]
        );
      }
      if (this.searchContent.pgTime) {
        data.eval_time_start = dateFormatter(
          "YYYY-MM-DD 00:00:00",
          this.searchContent.pgTime[0]
        );
        data.eval_time_end = dateFormatter(
          "YYYY-MM-DD 23:59:59",
          this.searchContent.pgTime[1]
        );
      }
      if(this.searchContent.isValid != ""){
        data.isValid = Number(this.searchContent.isValid)
      }
      console.log("data===============", data);
      this.$http
        .post("/admin/activity/index.php?act=car_eval&op=list", data)
        .then((res) => {
          if (res.code == 1) {
            if (res.data) {
              this.dataList = res.data.list;
              this.total = parseInt(res.data.totalNum);
            } else {
              this.dataList = [];
              this.total = 0;
            }
          } else if (res.code == 2001) {
            this.dataList = [];
            this.total = 0;
          } else {
            alert(res.msg);
          }
        });
    },
    search() {
      this.getList();
    },

    // 删除店铺
    deleter(id,status) {
      var str;
      if(status==2){
        str="确认审核通过？"
      }else if(status==3){
        str="确认审核不通过？"
      }
      this.$confirm(str)
        .then((_) => {
          this.$http
            .post("/admin/store_sms/index.php?act=templateAudit&op=audit", {
              id: id,
              status:status,
            })
            .then(
              (res) => {
                if (res.code == 1) {
                  this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "success",
                  });
                  this.getList();
                } else {
                  this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "error",
                  });
                }
              },
              (err) => {
                this.$message({
                  showClose: true,
                  message: res.msg,
                  type: "error",
                });
              }
            );
          done();
        })
        .catch((_) => {});
    },
    // 点击编辑
    edit(storeInfo) {
      this.$refs.ceshi.show_dialog_fn(storeInfo);
    },
  },
  mounted() {
    resize(this);
  },
};
</script>
<style lang="scss" scoped>
.search-content {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  &-input {
    width: 200px;
  }
  .putaway-time-wrapper {
    .putaway-time-label {
      font-size: 14px;
    }
    display: flex;
    align-items: center;
    /deep/.el-range-separator {
      box-sizing: content-box;
    }
  }
}
.bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
